import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import {Company} from '@/base/model/Company';
import qs from 'qs';
import {CompanyAccountVo} from '@/base/model/CompanyAccountVo';
import {CompanyAccount} from '@/base/model/CompanyAccount';

interface CompanyVM {
  address: string,
  city: string,
  cityId: string,
  clientUrl: string,
  district: string,
  districtId: string,
  name: string,
  phone: string,
  province: string,
  provinceId: string,
  shortName: string
}

export function getCompanyList(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection: string,
  name?: string,
  createBegin?: string,
  createEnd?: string
}): Promise<Result<Page<Company>>> {
  return request.getPage(Company, '/api/company', {params});
}

export function getCompanyAccountList(params: {
  pageIndex: number,
  pageSize: number,
  name?: string,
  email?: string,
  phone?: string,
  createBegin?: string,
  createEnd?: string
}): Promise<Result<Page<CompanyAccount>>> {
  return request.getPage(CompanyAccount, '/api/company/account', {params});
}

export function getCompany(id: number): Promise<Result<CompanyAccountVo>> {
  return request.get(CompanyAccountVo, `/api/company/${id}`);
}

export function putCompany(id: number, params: CompanyVM): Promise<Result<Company>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Company, `/api/company/${id}`, formData);
}

export function postCompany(params: CompanyVM): Promise<Result<Company>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Company, '/api/company', formData);
}

export function deleteCompany(id: number): Promise<Result<void>> {
  return request.delete(`/api/company/${id}`);
}

export function putEnable(params: {
  companyId: number, enable: boolean
}): Promise<Result<Company>> {
  return request.put(Company, '/api/company/enable', qs.stringify(params));
}
